<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<body>

<div th:fragment="nav">
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">码乎社区</span>
                </button>
                <a class="navbar-brand" href="/">码乎社区</a>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

                <span class="hidden" id="categoryId" th:text="${categoryId}"></span>
                <ul class="nav navbar-nav" id="categoryList">
                    <li th:each="category : ${categorys}" th:class="${category.id} == ${categoryId}?'active':''">
                        <a th:href="${category.id} == ${categoryId}? '#' : @{'/?category='+ ${category.id}}" th:text="${category.name}"></a>
                    </li>
                </ul>

                <form class="navbar-form navbar-left" action="/" method="get">
                    <div class="form-group">
                        <input type="text" class="form-control" name="search" placeholder="搜索问题">
<!--                        <input type="hidden" name="category" id="categoryInput">-->
                    </div>
                    <button type="submit" class="btn btn-default" id="search-btn">搜索</button>
                </form>
                <ul class="nav navbar-nav">
                    <script>

                        function clickNav(label) {
                            gtag('event', 'click', {
                                'event_category': 'LINK',
                                'event_label': label,
                                'transport_type': 'NAV'
                            })
                        }

                        //获取url中的参数值
                        function GetUrlParam(paraName) {
                            var url = document.location.toString();
                            var arrObj = url.split("?");

                            if (arrObj.length > 1) {
                                var arrPara = arrObj[1].split("&");
                                var arr;

                                for (var i = 0; i < arrPara.length; i++) {
                                    arr = arrPara[i].split("=");

                                    if (arr != null && arr[0] == paraName) {
                                        return arr[1];
                                    }
                                }
                                return "";
                            }
                            else {
                                return "";
                            }
                        }

                        $("#search-btn").mousedown(function () {//用于点击保存之后将搜索问题的类别的选择加到隐藏表单域里面提交
                            $('#categoryInput').val(GetUrlParam('category'));
                        });

                        // $(function () {
                        //     // 获取请求参数
                        //     var categoryId = GetUrlParam("category");
                        //
                        //     var categoryStr="";
                        //
                        //     // 执行ajax
                        //     $.ajax({
                        //         type: "POST",
                        //         url: "/getCategory",
                        //         contentType: 'application/json',
                        //         async: true, //同步请求
                        //         data: JSON,
                        //         cache: false,
                        //         timeout: 6000,
                        //         success: function (response) {
                        //             if (response.code == 200) {
                        //                 if(categoryId == 0){
                        //                     categoryId = response.data[0].id;
                        //                 }
                        //                 $.each(response.data,function(index, category){
                        //                     if(category.id == categoryId){
                        //                         categoryStr += "<li class='active'>" + "<a href='/?category=" + category.id + "'>" + category.name + "</a></li>"
                        //                     }else{
                        //                         categoryStr += "<li>" + "<a href='/?category=" + category.id + "'>" + category.name + "</a></li>"
                        //                     }
                        //                     $("#categoryList").html(categoryStr);
                        //                 });
                        //             } else {
                        //                 var isAccepted = confirm(response.message);
                        //                 if (isAccepted) {
                        //                      window.location.reload();
                        //                 }
                        //             }
                        //         },
                        //         dataType: "json"
                        //     });
                        // })
                    </script>
                    <li th:each="ad : ${#httpServletRequest.getServletContext().getAttribute('NAV')}">
                        <a th:href="${ad.url}" th:text="${ad.title}" th:onclick="clickNav([[${ad.title}]])"
                           target="_blank"></a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li th:if="${session.user != null}">
                        <a href="/publish">提问</a>
                    </li>
                    <li th:if="${session.user!=null}"><a href="/profile/replies">通知 <span class="badge" th:text="${session.unreadCount}"></span></a>
                    </li>
                    <li class="dropdown" th:if="${session.user != null}">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">
                            <span th:text="${session.user.getName()}"></span>
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="/profile/account">个人空间</a></li>
                            <li><a href="/logout">退出登录</a></li>
                        </ul>
                    </li>
                    <li th:if="${session.user == null}">
                        <a href="/user/loginPage">登录</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</div>

</body>

</html>
